package com.huifeng.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BeanServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //下面我们想要使用我们的反射的功能,来让我们方法的调用更加的智能
        //下面需要判断根据用户需求来获取想要的方法
        String method = req.getParameter("method");
        //获取到方法名之后,我们需要获取当前以及子类的字节码对象
        Class<? extends BeanServlet> clazz = this.getClass();
        //我们需要获取我们想要的具体的方法对象
        try {
            Method mh = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            //执行方法
            mh. invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
